package com.owlsmart.bluecat.fragment;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.Gson;
import com.owlsmart.bluecat.R;
import com.owlsmart.bluecat.adapter.HomeAdapter;
import com.owlsmart.bluecat.api.Api;
import com.owlsmart.bluecat.api.ApiConfig;
import com.owlsmart.bluecat.api.TitleCallback;
import com.owlsmart.bluecat.entity.CategoryEntity;
import com.owlsmart.bluecat.entity.VideoCategoryResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class HomeFragment extends BaseFragment {

    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private String[] mTitles = {"热门", "电影", "电视剧", "动漫", "综艺", "爱情"};

    private ViewPager viewPager;
    private SlidingTabLayout slidingTabLayout;

    public HomeFragment() {
    }

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {

        viewPager = mRootView.findViewById(R.id.fixedViewPager);
        slidingTabLayout = mRootView.findViewById(R.id.slidingTabLayout);
    }

    @Override
    protected void initDate() {
        for (String title : mTitles) {
            mFragments.add(MovieListFragment.newInstanceTitle(title));

        }
        // 预加载顶部导航栏下标，防止出现数组下标越界异常
        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.setAdapter(new HomeAdapter(getFragmentManager(), mTitles, mFragments));
        slidingTabLayout.setViewPager(viewPager);
    }

    // 加载接口获取内容
    private void getVideoCategoryLIst() {

        HashMap<String, Object> params = new HashMap<>();
        // params.put("token", token);
        Api.config(ApiConfig.VIDEO_CATEGORY_LIST, params).getRequest(getActivity(), new TitleCallback() {
            @Override
            public void onSuccess(String res) {
                final VideoCategoryResponse response = new Gson().fromJson(res, VideoCategoryResponse.class);
                if (response != null && response.getCode() == 0) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            List<CategoryEntity> list = response.getPage().getList();
                            if (null != list && list.size() > 0) {
                                mTitles = new String[list.size()];
                                for (int i = 0; i < list.size(); i++) {
                                    mTitles[i] = list.get(i).getCategoryName();
                                    // 从网络获取的标题赋值给mTitles后，添加到mFragments
                                    mFragments.add(VideoFragment.newInstance(list.get(i).getCategoryId()));
                                }
                                // 预加载顶部导航栏下标，防止出现数组下标越界异常
                                viewPager.setOffscreenPageLimit(mFragments.size());
                                viewPager.setAdapter(new HomeAdapter(getFragmentManager(), mTitles, mFragments));
                                slidingTabLayout.setViewPager(viewPager);
                            }
                        }
                    });
                } else {
                    showToastSync("初始化数据异常~");
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}